package online.shenme;

import java.util.Random;
import java.util.Scanner;

//100以内随机数求和
class SumTest {
    public static void main(String[] args) {
        Random random = new Random();
        int r = random.nextInt(100);
        System.out.println("随机数：" + r);
        System.out.println("依次求和：" + sum(r));
    }

    public static int sum(int x) {
        int sum = 0;
        for (int i = 0; i <= x; i++) {
            sum += i;
        }
        return sum;
    }
}

//NN乘法表
class ChenFaxls {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入N的值：");
        int n = input.nextInt();
        cfb(n);
    }

    public static void cfb(int n) {
        //判断输入是否正确，输入错误结束方法
        if (n <= 0 || n >= 10) {
            System.out.println("输入错误");
            return;
        }
        //打印乘法表
        int i, j;
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= n; j++) {
                //修改判断条件可以修改显示方向。
                if (i <= j) {
                    int ji = i * j;
                    System.out.print(i + "*" + j + "=" + ji + "\t");
                }
            }
            System.out.println();
        }
    }
}

//遍历打印随机数组
class ArrayPrint {
    public static void main(String[] args) {
        int index = 1000;
        int[] arr = new int[index];
        System.out.println("自动生成的数组数据");
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100);
        }
        //方法调用，将数组排序
        long start = System.currentTimeMillis();
        sort(arr);
//        arr(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "\t");
            if ((i + 1) % 5 == 0) {
                System.out.println();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }

    //找出数组中的最大值与最小值
    public static void arr(int[] arr) {
        int max = arr[0], min = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max)
                max = arr[i];
            else min = arr[i];
        }
        System.out.println("最大值：" + max);
        System.out.println("最小值：" + min);
    }

    //数组的排序
    public static void sort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }
    //数组的冒泡排序
    public static void bubleSort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j]>arr[j+1]){
                    int temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }
    }


    //数组的随机化
    public static void random(int[] arr) {
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            int index = random.nextInt(arr.length);
            int temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
        }
    }
}
